package cz.schovjan.mte_proj.activity;

import java.util.List;
import java.util.Random;

import org.achartengine.ChartFactory;
import org.achartengine.model.CategorySeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import cz.schovjan.mte_proj.R;
import cz.schovjan.mte_proj.dto.Artist;
import cz.schovjan.mte_proj.fragment.SearchByArtistSongFragment;
import cz.schovjan.mte_proj.fragment.SearchByTextFragment;
import cz.schovjan.mte_proj.fragment.SearchListFragment;
import cz.schovjan.mte_proj.service.Database;
import cz.schovjan.mte_proj.service.DatabaseImpl;
import cz.schovjan.mte_proj.service.Searcher;

public class MainActivity extends Activity implements SearchListFragment.SearchTypeCallBack {

    private boolean detailPane = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	detailPane = findViewById(R.id.fr_detail) != null;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
	getMenuInflater().inflate(R.menu.main, menu);
	return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
	switch (item.getItemId()) {
	    case R.id.menu_show_graph:
		Intent intent = getGraphIntent(this);
		startActivity(intent);
		return true;
	    default:
		return super.onOptionsItemSelected(item);
	}
    }


    public Intent getGraphIntent(Context context) {
	Database db = new DatabaseImpl(this);
	List<Artist> as = db.findAllArtist();
	double[] values = new double[as.size()];
	int[] colors = new int[as.size()];
	for (int i = 0; i < as.size(); i++) {
	    values[i] = as.get(i).getCount();
	    Random randomGenerator = new Random();
	    int red = randomGenerator.nextInt(255);
	    int green = randomGenerator.nextInt(255);
	    int blue = randomGenerator.nextInt(255);
	    colors[i] = Color.rgb(red, green, blue);
	}
	DefaultRenderer renderer = buildCategoryRenderer(colors);
	renderer.setZoomButtonsVisible(true);
	renderer.setZoomEnabled(true);
	renderer.setChartTitleTextSize(20);
	return ChartFactory.getPieChartIntent(context, buildCategoryDataset(getResources().getString(R.string.stat), as),
		renderer, getResources().getString(R.string.graph));
    }


    private DefaultRenderer buildCategoryRenderer(int[] colors) {
	DefaultRenderer renderer = new DefaultRenderer();
	renderer.setLabelsTextSize(10);
	renderer.setLegendTextSize(15);
	renderer.setMargins(new int[] { 5, 5, 5, 5 });
	for (int color : colors) {
	    SimpleSeriesRenderer r = new SimpleSeriesRenderer();
	    r.setColor(color);
	    renderer.addSeriesRenderer(r);
	}
	return renderer;
    }


    private CategorySeries buildCategoryDataset(String title, List<Artist> as) {
	CategorySeries series = new CategorySeries(title);
	for (int i = 0; i < as.size(); i++) {
	    series.add(as.get(i).getArtist()+" ("+as.get(i).getCount()+")", as.get(i).getCount());
	}
	return series;
    }


    @Override
    public void onSearchTypeChoose(int type) {
	switch (type) {
	    case Searcher.SEARCH_BY_ARTIST_SONG:
		// when layout landscape show detail in fragment, otherwise start detail activity
		if (detailPane) {
		    // when using support library, use getSupportFragmentManager();
		    FragmentManager fm = getFragmentManager();
		    Fragment detail = SearchByArtistSongFragment.createInstance();
		    // Fragment detail = DetailFragment.createInstance("sdkfjkjsdfs");
		    fm.beginTransaction()
			    .replace(R.id.fr_detail, detail)
			    .addToBackStack(null)
			    .commit();
		} else {
		    Intent detail = new Intent(this, SearchByArtistSongActivity.class);
		    // Intent detail = new Intent(this, DetailActivity.class);
		    startActivity(detail);
		}
		break;
	    case Searcher.SEARCH_BY_TEXT:
		// when layout landscape show detail in fragment, otherwise start detail activity
		if (detailPane) {
		    // when using support library, use getSupportFragmentManager();
		    FragmentManager fm = getFragmentManager();
		    Fragment detail = SearchByTextFragment.createInstance();
		    fm.beginTransaction()
			    .replace(R.id.fr_detail, detail)
			    .addToBackStack(null)
			    .commit();
		} else {
		    Intent detail = new Intent(this, SearchByTextActivity.class);
		    startActivity(detail);
		}
		break;
	    default:
		break;
	}
    }
}
